﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Maps.MapControl.WPF;
using System.Runtime.Serialization;
using MayhemCore;
using MayhemWpf.ModuleTypes;
using MayhemWpf.UserControls;
using SensorMayhem.Wpf;
using System.Device.Location;

namespace SensorMayhem.Events
{

    [Serializable]
    public class LocationOptions
    {
        public double Radius { get; set; }
        public Microsoft.Maps.MapControl.WPF.Location Location { get; set; }
        public bool Inside { get; set; }
    }


    [DataContract]
    [MayhemModule("Sensor: Location", "Triggers when the computer is in a specific geographic area")]
    public class Location : EventBase, IWpfConfigurable
    {

        [DataMember]
        private LocationOptions options;

        private GeoCoordinateWatcher geoWatcher;

        private bool triggered = false;


        protected override void OnLoadDefaults()
        {
            options = new LocationOptions() { Radius = 0.5, Inside = true };
        }

        protected override void OnEnabling(EnablingEventArgs e)
        {
            try
            {
                geoWatcher = new GeoCoordinateWatcher();
                geoWatcher.PositionChanged += new EventHandler<GeoPositionChangedEventArgs<GeoCoordinate>>(geoWatcher_PositionChanged);
                geoWatcher.TryStart(true, TimeSpan.FromMilliseconds(5000)); 
            }
            catch (Exception)
            {

            }

        }

        void geoWatcher_PositionChanged(object sender, GeoPositionChangedEventArgs<GeoCoordinate> e)
        {
            bool trigger = e.Position.Location.GetDistanceTo(new GeoCoordinate(options.Location.Latitude, options.Location.Longitude)) / 1000 < options.Radius;
            if (!options.Inside)
            {
                trigger = !trigger;
            }

            if (!triggered && trigger)
            {
                Trigger();
                triggered = true;
            }
            else if (!trigger)
            {
                ResetTrigger();
            }
        }

        void ResetTrigger()
        {
            triggered = false;
        }

        protected override void OnDisabled(DisabledEventArgs e)
        {
            try
            {
                geoWatcher.PositionChanged -= new EventHandler<GeoPositionChangedEventArgs<GeoCoordinate>>(geoWatcher_PositionChanged);
            }
            catch (Exception)
            {

            }
            ResetTrigger();
        }

        public WpfConfiguration ConfigurationControl
        {
            get
            {
                return new LocationConfig(options);
            }
        }

        public void OnSaved(WpfConfiguration configurationControl)
        {
            LocationConfig config = configurationControl as LocationConfig;
            options = config.Options;
        }

        public string GetConfigString()
        {
            return "Location: " + options.Location.ToString();
        }
    }
}
